package com.gitee.huanminabc.dynamicdb.poll;

import com.gitee.huanminabc.dynamicdb.common.Constants;
import com.gitee.huanminabc.dynamicdb.core.DBContext;
import com.gitee.huanminabc.dynamicdb.core.TransactionalManage;

import java.util.concurrent.*;
/**
 * @author huanmin
 * @date 2024/11/22
 */
public class ShareContextPollExecutor extends ThreadPoolExecutor {
    public ShareContextPollExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
       super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public ShareContextPollExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public ShareContextPollExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public ShareContextPollExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }
    @Override
    public void execute(Runnable command) {
        super.execute(()->{
            try {
                TransactionalManage.clearTransaction();
                //判断是否有数据key没有给一个默认的
                if (DBContext.dbKeys.get() == null) {
                    DBContext.dbKeys.set(Constants.DEFAULT_DB_KEY);
                }
                command.run();
            } finally {
                DBContext.clear();//线程结束的时候清除数据源,防止线程池线程复用导致数据源共享问题
            }
        });
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return super.submit(() -> {
            try {
                TransactionalManage.clearTransaction();
                //判断是否有数据key没有给一个默认的
                if (DBContext.dbKeys.get() == null) {
                    DBContext.dbKeys.set(Constants.DEFAULT_DB_KEY);
                }
                return task.call();
            } finally {
                DBContext.clear();//线程结束的时候清除数据源,防止线程池线程复用导致数据源共享问题
            }
        });
    }
    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(()->{
            try {
                TransactionalManage.clearTransaction();
                //判断是否有数据key没有给一个默认的
                if (DBContext.dbKeys.get() == null) {
                    DBContext.dbKeys.set(Constants.DEFAULT_DB_KEY);
                }
                task.run();
            } finally {
                DBContext.clear();//线程结束的时候清除数据源,防止线程池线程复用导致数据源共享问题
            }
        });
    }

    @Override
    public <T> Future<T> submit(Runnable task, T result) {
        return super.submit(()->{
            try {
                TransactionalManage.clearTransaction();
                //判断是否有数据key没有给一个默认的
                if (DBContext.dbKeys.get() == null) {
                    DBContext.dbKeys.set(Constants.DEFAULT_DB_KEY);
                }
                task.run();
            } finally {
                DBContext.clear();//线程结束的时候清除数据源,防止线程池线程复用导致数据源共享问题
            }
        }, result);
    }

}
